package com.example.demo.repository;

import com.example.demo.entity.SchoolClass;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.data.repository.query.Param;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface SchoolClassRepository extends R2dbcRepository<SchoolClass, Long> {

    @Query("SELECT * FROM t_class WHERE grade =:grade_id order by id")
    Flux<SchoolClass> queryAllBySchoolGrade(Long grade_id);

    @Modifying
    @Query( "insert into t_class(id,name,grade) VALUES (:#{#schoolClass.id},:#{#schoolClass.name},:#{#schoolClass.grade})"+
            "on conflict (id) do update SET " +
            "name = CASE WHEN :#{#schoolClass.name} IS NULL THEN t_class.name ELSE :#{#schoolClass.name} END, " +
            "grade = CASE WHEN :#{#schoolClass.grade} IS NULL THEN t_class.grade ELSE :#{#schoolClass.grade} END " )
    Mono<SchoolClass> insertOrUpdateClass(@Param("schoolClass") SchoolClass schoolClass);

}

